﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace WindowsFormsApplication1
{
    public static class Email
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="emailServer">邮件服务器</param>
        /// <param name="fromAddess">发件地址</param>
        /// <param name="passworld">发件地址的密码</param>
        /// <param name="toUsers">收件人列表</param>
        /// <param name="title">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <returns></returns>
        public static void SendEmail(string emailServer, string fromAddess, string passworld, string[] toUsers, string title, string body,string sAttachFile)
        {
            emailServer = emailServer.Trim();
            fromAddess = fromAddess.Trim();
            title = title.Trim();

            if (string.IsNullOrEmpty(emailServer) || string.IsNullOrEmpty(fromAddess) || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(body) || toUsers == null || toUsers.Length == 0)
                throw new Exception("参数错误：邮件信息不齐全！");

            // 获取用户名
            int i = fromAddess.IndexOf("@");
            string username = fromAddess; //fromAddess.Substring(0, i);


            // 创建邮件对象
            MailMessage mmsg = new MailMessage();
            // 添加发件人
            mmsg.From = new MailAddress(fromAddess);
            // 添加收件人
            foreach (string str in toUsers)
            {
                mmsg.To.Add(str);
            }
            // 邮件主题
            mmsg.Subject = title;
            // 邮件内容
            mmsg.Body = body;
            // 是否html
            mmsg.IsBodyHtml = false;
            // 优先级
            if (title.Contains("问题"))
                mmsg.Priority = MailPriority.High;
            else
                mmsg.Priority = MailPriority.Normal;
            // 设置邮件主题的编码格式
            mmsg.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
            // 设置邮件内容的编码格式
            mmsg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            // 附件
            if (!string.IsNullOrEmpty(sAttachFile))
            {
                mmsg.Attachments.Add(new Attachment(sAttachFile));
            }

            // 创建发送客户端
            SmtpClient smtp = new SmtpClient();
            
            smtp.Host = emailServer;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(username, passworld);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtp.Send(mmsg);
            mmsg.Dispose();
            

        }


        /// <summary>
        /// 发送邮件  区分是否HTML格式
        /// </summary>
        /// <param name="emailServer"></param>
        /// <param name="fromAddess"></param>
        /// <param name="passworld"></param>
        /// <param name="toUsers"></param>
        /// <param name="title"></param>
        /// <param name="body"></param>
        /// <param name="sAttachFile"></param>
        /// <param name="ifBodyHtml"></param>
        public static void SendEmailHtml(string emailServer, string fromAddess, string passworld, string[] toUsers, string title, string body, string sAttachFile,bool ifBodyHtml)
        {
            emailServer = emailServer.Trim();
            fromAddess = fromAddess.Trim();
            title = title.Trim();

            if (string.IsNullOrEmpty(emailServer) || string.IsNullOrEmpty(fromAddess) || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(body) || toUsers == null || toUsers.Length == 0)
                throw new Exception("参数错误：邮件信息不齐全！");

            // 获取用户名
            int i = fromAddess.IndexOf("@");
            string username = fromAddess.Substring(0, i);


            // 创建邮件对象
            MailMessage mmsg = new MailMessage();
            // 添加发件人
            mmsg.From = new MailAddress(fromAddess);
            // 添加收件人
            foreach (string str in toUsers)
            {
                mmsg.To.Add(str);
            }
            // 邮件主题
            mmsg.Subject = title;
            // 邮件内容
            mmsg.Body = body;
            // 是否html
            mmsg.IsBodyHtml = ifBodyHtml;
            // 优先级
            if (title.Contains("问题"))
                mmsg.Priority = MailPriority.High;
            else
                mmsg.Priority = MailPriority.Normal;
            // 设置邮件主题的编码格式
            mmsg.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
            // 设置邮件内容的编码格式
            mmsg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            // 附件
            if (!string.IsNullOrEmpty(sAttachFile))
            {
                mmsg.Attachments.Add(new Attachment(sAttachFile));
            }

            // 创建发送客户端
            SmtpClient smtp = new SmtpClient();

            smtp.Host = emailServer;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(username, passworld);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtp.Send(mmsg);
            mmsg.Dispose();


        }

    
    }
}
